package com.whj.wkpt.service;

import com.whj.wkpt.entity.DeliveryAttachment;
import com.whj.wkpt.mapper.DeliveryAttachmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeliveryAttachmentService {

    @Autowired
    private DeliveryAttachmentMapper deliveryAttachmentMapper;

    public DeliveryAttachment createAttachment(DeliveryAttachment attachment) {
        deliveryAttachmentMapper.insert(attachment);
        return attachment;
    }

    public DeliveryAttachment findById(Long attachmentId) {
        return deliveryAttachmentMapper.findById(attachmentId);
    }

    public List<DeliveryAttachment> findByApplicationId(Long applicationId) {
        return deliveryAttachmentMapper.findByApplicationId(applicationId);
    }

    public void deleteAttachment(Long attachmentId) {
        deliveryAttachmentMapper.deleteById(attachmentId);
    }

    public void deleteByApplicationId(Long applicationId) {
        List<DeliveryAttachment> attachments = findByApplicationId(applicationId);
        for (DeliveryAttachment attachment : attachments) {
            deleteAttachment(attachment.getAttachmentId());
        }
    }
}